package com.woniu.shop.service.impl;
import java.math.BigDecimal;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.Result;

import com.woniu.UserFeignClient;
import com.woniu.pojo.User;
import com.woniu.shop.pojo.Shop;
import com.woniu.shop.mapper.ShopMapper;
import com.woniu.shop.pojo.Application;
import com.woniu.shop.mapper.ApplicationMapper;
import com.woniu.shop.pojo.vo.AcceptApplicationVo;
import com.woniu.shop.pojo.vo.AllApplicationVo;
import com.woniu.shop.pojo.vo.ApplicationAcceptDto;
import com.woniu.shop.pojo.vo.ApplicationVo;
import com.woniu.shop.rabibit.ApplicationProduct;
import com.woniu.shop.rabibit.ConfirmProducer;
import com.woniu.shop.service.IApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.shop.utils.GetPhone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohuang
 * @since 2022-11-16
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements IApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ApplicationProduct applicationProduct;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ConfirmProducer confirmProducer;

    @Autowired
    private UserFeignClient userFeignClient;


    /**
     * 添加
     * @param applicationVo
     * @return
     */
    @Override
    public Result<String> saveApplication(ApplicationVo applicationVo) {
        Integer userId = applicationVo.getShop().getUserId();
        List<Integer> list =applicationMapper.checkAuditStatus(userId);
        if(CollectionUtils.isNotEmpty(list)){
            return Result.fail("请等待门店审核成功后在申请");
        }
        Application application = applicationVo.getApplication();
        Shop shop = applicationVo.getShop();
        application.setApplicationTime(new Date());
        int row01 = shopMapper.insert(shop);
        application.setShopId(shop.getId());
        int row02 = applicationMapper.insert(application);

        if (row01 > 0 && row02 > 0) {
            stringRedisTemplate.opsForSet().add("redis_db_upload_image",shop.getCertificateImg());
            stringRedisTemplate.opsForSet().add("redis_db_upload_image",shop.getIdImg());
            stringRedisTemplate.opsForSet().add("redis_db_upload_image",shop.getShopImg());

//            applicationProduct.sendMessage(application.getId());
            confirmProducer.producerConfirm(application.getId());
            return Result.ok();

        } else {
            return Result.fail();
        }

    }

    /**
     * 通过id查找
     * @param id
     * @return
     */
    @Override
    public ApplicationVo findById(Integer id) {
        Application application = applicationMapper.selectById(id);
        Integer shopId = application.getShopId();
        Shop shop = shopMapper.selectById(shopId);
        ApplicationVo applicationVo = new ApplicationVo();
        applicationVo.setApplication(application);
        applicationVo.setShop(shop);
        return applicationVo;
    }

    /**
     * 查询所有
     * @param userId
     * @return
     */
    @Override
    public List<AllApplicationVo> findAll(Integer userId) {
        List<AllApplicationVo> list = applicationMapper.findAll(userId);
        return list;
    }

    /**
     * 修改
     * @param applicationVo
     * @return
     */
    @Override
    public Result<String> modify(ApplicationVo applicationVo) {
        Application application = applicationVo.getApplication();
        Integer auditStatus = this.getById(application.getId()).getAuditStatus();
        if (auditStatus==1){
            return Result.fail("审核中，请稍后再试");
        }
        application.setApplicationTime(new Date());
        Shop shop = applicationVo.getShop();
        application.setAuditStatus(1);
        int i01 = applicationMapper.updateById(application);
        int i02 = shopMapper.updateById(shop);
        if (i01 > 0 && i02 > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Override
    public Result<String> oneAudit(Application application) {
        UpdateWrapper<Application> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",application.getId());
        if (application.getOneAuditResult() == 0){
            application.setAuditStatus(2);
        } else {
            application.setAuditStatus(1);
        }
        int i = applicationMapper.update(application, wrapper);
        if (i > 0){
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @Override
    public Result<String> twoAudit(Application application) {
        UpdateWrapper<Application> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",application.getId());
        application.setAuditStatus(3);
        int i = applicationMapper.update(application, wrapper);
        if (i > 0){
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Override
    public Result<AcceptApplicationVo> accept(ApplicationAcceptDto dto) {
        Application application =
                applicationMapper.selectOne(Wrappers.lambdaQuery(Application.class).eq(Application::getShopId, dto.getShopId()));
        if(application.getAuditStatus()!=3){
            return Result.fail();
        }
        Shop shop = shopMapper.selectById(dto.getShopId());
        if(shop.getStatus()==1){
            return Result.fail();
        }
        shop.setStatus(1);
        shopMapper.updateById(shop);
        String tel = GetPhone.getPhone();
        String Password = "";
        for (int i = 0; i < 6; i++) {
            int random = (int)(Math.random()*10);
            Password = Password + random;
        }
        com.woniu.pojo.User user = new User();
        String gensalt = BCrypt.gensalt();
        user.setPassword(BCrypt.hashpw(Password,gensalt));
        user.setTel(tel);
        user.setMoney(new BigDecimal("0"));
        user.setType(2);
        userFeignClient.add(user);
        AcceptApplicationVo acceptApplicationVo = new AcceptApplicationVo();
        acceptApplicationVo.setTel(tel);
        acceptApplicationVo.setPassword(Password);
        return Result.ok(acceptApplicationVo);
    }
}
